package com.jettech.oa.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.jettech.oa.dao.WorkFlowDAO;
import com.jettech.oa.domain.FormProperty;
import com.jettech.oa.domain.WorkFlow;
import com.jettech.oa.domain.WorkFlowForm;
import com.jettech.oa.service.JbpmService;
import com.jettech.oa.service.WorkFlowService;
import com.jettech.oa.utils.PageUtil;
/**
 * 流程服务接口实现类
 * @author Administrator
 *
 */
@Transactional
@Service("workFlowService")
public class WorkFlowServiceImpl implements WorkFlowService {
	@Autowired
	private WorkFlowDAO workFlowDAO;
	@Autowired
	private JbpmService jbpmService;
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteWorkFlow(Long id) {
		WorkFlow workFlow = workFlowDAO.findById(id);
		if(workFlow!=null ){
			if(workFlow.getForms()!=null && workFlow.getForms().size()>0){
				workFlowDAO.deleteWorkFlowForm(workFlow.getId());
			}
			workFlowDAO.makeTransient(workFlow);
		}
	}

	@Transactional(readOnly=true)
	public List<WorkFlow> findWorkFlowList(PageUtil pageUtil) {
		//String hql = "";
		return workFlowDAO.findAll();
	}

	@Transactional(readOnly=true)
	public List<WorkFlow> findWorkFlowList() {
		return workFlowDAO.findAll();
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void saveOrUpdateWorkFlow(WorkFlow workFlow) {
		if(workFlow!=null ){
			if(workFlow.getProcessDefinedFile()!=null){
				//部署流程定义
				String processDefinitionName = jbpmService.saveProcessDefinition(workFlow.getProcessDefinedFile());
				workFlow.setName(processDefinitionName);
				workFlowDAO.makePersistent(workFlow);
			}
		}
	}

	@Transactional(readOnly=true)
	public boolean isUniqueName(String name) {
		String hql = "select count(id) from WorkFlow where name = :workFlowName";
		Map<String,String> params = new HashMap<String,String>();
		params.put("workFlowName", name);
		int sum = this.workFlowDAO.countHqlQuery(hql, params);
		return sum>1?false:true;
	}

	@Transactional(propagation=Propagation.REQUIRED)
	public void saveWorkFlowForm(WorkFlowForm form) {
		workFlowDAO.getHibernateTemplate().saveOrUpdate(form);
		if(form.getFormProperties()!=null && form.getFormProperties().size()>0){
			workFlowDAO.deleteWorkFlowFormProperty(form.getId());
			for(FormProperty property:form.getFormProperties()){
				workFlowDAO.getHibernateTemplate().save(property);
			}
		}
	}

	@Transactional(readOnly=true)
	@SuppressWarnings("unchecked")
	public List<WorkFlowForm> findWorkFlowFormList(Long workFlowId) {
		String hql = "from WorkFlowForm where workFlow.id = ? ";
		return workFlowDAO.getHibernateTemplate().find(hql, workFlowId);
	}

	public WorkFlowForm getWorkFlowForm(Long id) {
		return workFlowDAO.getHibernateTemplate().load(WorkFlowForm.class, id);
	}

}
